package com.wandian.ui.controls
{
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.GlowFilter;
	import flash.utils.getQualifiedClassName;
	import flash.utils.getTimer;
	

	use namespace ui_internal;

	public class UIComponent extends Sprite
	{

		protected var manager:UIManager
		public static var focusFilter:GlowFilter=new GlowFilter();
		private var invalidateSizeFlag:Boolean=false;
		private var invalidateDisplayFlag:Boolean=false;
		private var _width:int;
		private var _height:int;
		private var _enabled:Boolean=true
		private var initialized:Boolean=false
		private var className:String;
		private var classStyle:ClassStyleDeclaration;
		private var builtinStyle:Object
		private var styleDeclaration:StyleDeclaration


		public function UIComponent(w:int=100, h:int=100):void
		{
			_width=w;
			_height=h;
			manager=UIManager.getInstance();
			className=getQualifiedClassName(this);
			classStyle=manager.getClassStyleDeclaration(className);
		}


		public function initialize():void
		{
			if (initialized)
			{
				return;
			}
			initialized=true
			var startTimer:int=getTimer();
			createChildren()
			this.invalidateSize(true)
			this.invalidateDisplay(true)
			trace(className + " Created:" + (getTimer() - startTimer) + "ms");

		}

		protected function createChildren():void
		{

		}

		protected function layoutContents(w:int, h:int):void
		{

		}

		protected function updateDisplay(w:int, h:int):void
		{

		}

		private function getClassStyleDeclaration():void
		{

		}

		public function getStyle(key:String):*
		{
			var v:*;
			if (builtinStyle != null)
			{
				v=builtinStyle[key]
				if (v != null)
				{
					return v
				}
			}
			if (styleDeclaration != null)
			{
				v=styleDeclaration.getStyle(key)
				if (v != null)
				{
					return v
				}
			}
			return classStyle.getStyle(key);

		}

		public function setStyle(key:String, v:*):void
		{
			if (builtinStyle == null)
			{
				builtinStyle={}
			}
			builtinStyle[key]=v
			styleChanged(key, v)

		}

		public function clearStyle(key:String):void
		{
			if (builtinStyle == null)
			{
				return;
			}
			builtinStyle[key]=null;
		}

		public function invalidateSize(now:Boolean=false):void
		{
			if(now){
				validateSize();
				return;
			}
			if (invalidateSizeFlag)
			{
				return;
			}
			invalidateSizeFlag=true;
			manager.invalidateSize(this)
		}

		ui_internal function validateSize():void
		{
			invalidateSizeFlag=false;
			layoutContents(this._width, this._height)
		}

		public function invalidateDisplay(now:Boolean=false):void
		{
			if(now){
				validateDisplay();
				return;
			}
			if (invalidateDisplayFlag)
			{
				return;
			}
			invalidateDisplayFlag=true;
			manager.invalidateDisplay(this)
		}

		ui_internal function validateDisplay():void
		{
			invalidateDisplayFlag=false;
			updateDisplay(this._width, this._height)
		}

		public function getStyleDeclaration():StyleDeclaration
		{
			return styleDeclaration
		}

		public function setStyleDeclaration(v:StyleDeclaration):void
		{
			styleDeclaration=v
		}

		override public function get width():Number
		{
			return _width;
		}

		override public function set width(v:Number):void
		{
			if (int(v) == _width)
			{
				return
			}
			_width=int(v)
			this.invalidateSize()
			this.invalidateDisplay();
		}

		override public function get height():Number
		{
			return _height
		}

		override public function set height(v:Number):void
		{
			if (int(v) == _height)
			{
				return
			}
			_height=int(v)
			this.invalidateSize()
			this.invalidateDisplay();
		}

		protected function styleChanged(key:String, v:*):void
		{
		}

		public function get enabled():Boolean
		{
			return _enabled
		}

		public function set enabled(v:Boolean):void
		{
			if (_enabled == v)
			{
				return
			}
			_enabled=v;
			invalidateDisplay();
		}

	}
}
